package arkkomeet

import com.sun.org.apache.bcel.internal.generic.RETURN;
import Arkkomeet.CatalogoParticipante
import Arkkomeet.Minuta
import Arkkomeet.ParticipanteXMinuta

class MinutaParticipanteXMinutaService {

	def ArrayList<CatalogoParticipante> listaParticicpantes = new ArrayList<CatalogoParticipante>()

	def agregarParticipantes(Long CatPartid, Long Minid) {
		boolean existe = false
		def participanteXMinutaInstance = new ParticipanteXMinuta()

		def participante = new CatalogoParticipante().get(CatPartid)
		participanteXMinutaInstance.catPartId = participante		

		def minuta = new Minuta().get(Minid)
		participanteXMinutaInstance.minId = minuta
		
		participanteXMinutaInstance.asistencia = 1	

		for(int a = 0; a < listaParticicpantes.size(); a++){	
			if(listaParticicpantes.getAt(a).equals(participante)){
				existe = true
			}
		}
		if(existe == false){
			if(!participanteXMinutaInstance.save(flush: true)){
				println "dfghjkl"
			}
		}
		return cargarParticipantes(minuta)
	}

	def eliminarParticipante(Long id){
		boolean existe = false
		def catalogoParticipanteInstance = CatalogoParticipante.get(id)
		for(int a = 0; a < listaParticicpantes.size(); a++){
			if(listaParticicpantes.get(a).getCatPartCorreoElectronico().equals(catalogoParticipanteInstance.getCatPartCorreoElectronico())){
				listaParticicpantes.remove(a)
			}
		}
		return listaParticicpantes
	}

	def guardarParticipantes(){
		def participanteXMinutaInstance = new ParticipanteXMinuta()

		def participante = new CatalogoParticipante().get(1)
		participanteXMinutaInstance.catPartId = participante
		participanteXMinutaInstance.asistencia = 1

		def minuta = new Minuta().get(1)
		participanteXMinutaInstance.minId = minuta

		if(!participanteXMinutaInstance.save(flush: true)){
			println "error"
		}
	}

	def cargarParticipantes(Minuta idMin){
		listaParticicpantes = new ArrayList<CatalogoParticipante>()
		ArrayList<ParticipanteXMinuta> listaParticipantesXMinuta = ParticipanteXMinuta.findAll("FROM ParticipanteXMinuta pxm where pxm.minId = ?",
				[idMin])

		for(int a = 0; a < listaParticipantesXMinuta.size(); a++){
		CatalogoParticipante idParticipante=listaParticipantesXMinuta.getAt(a).catPartId
			def catalogoParticipanteInstance = CatalogoParticipante.get(idParticipante.id)
			listaParticicpantes.add(catalogoParticipanteInstance)
		}
		return listaParticicpantes
	}
}

